/**
 * 
 */
package com.mobile2you.nci.android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.util.Linkify;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.mobile2you.nci.android.data.InitialDataLoader;
import com.mobile2you.nci.android.data.SqlHelper;
import com.mobile2you.nci.android.data.WebServicesRequest;
import com.mobile2you.nci.android.domain.Services;
import com.mobile2you.nci.android.utils.ListServicesAdapter;
import com.mobile2you.nci.android.utils.LoadImagePool;
import com.mobile2you.nci.android.view.LoaderView;

/**
 * @author JBromo
 * 
 */
public class MenuService extends Activity {

	private List<Services> listCentres = new ArrayList<Services>();
	private ListView listView;
	private ViewFlipper viewFlipper;
	private ListServicesAdapter adapter;
	private TextView titleCentres;

	View waitingview;
	ImageView imageView;

	TextView textCentre;

	private LoaderView loaderView;

	private class UpdateLoadCentresTask extends
			AsyncTask<String, String, List<Services>> {

		@Override
		protected List<Services> doInBackground(String... params) {
			boolean toupdate = SqlHelper.getInstance(MenuService.this)
					.toUpdate(SqlHelper.KEY_SERVICE);
			if (toupdate) {
				listCentres = WebServicesRequest.getServices(MenuService.this);
			}
			if (listCentres == null || listCentres.isEmpty()) {
				listCentres = SqlHelper.getInstance(getApplicationContext())
						.getLocalServices();
				if (listCentres.isEmpty()) {
					listCentres = InitialDataLoader
							.getServices(MenuService.this);
				}
			}

			return listCentres;
		}

		@Override
		protected void onPostExecute(List<Services> result) {
			super.onPostExecute(result);
			if (result != null && !result.isEmpty()) {
				listCentres = result;
				showListview();
				viewFlipper.removeView(loaderView);
			} else {
				loaderView.setErrorMessage();
			}

		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu_service);
		listView = (ListView) findViewById(R.id.list_centre);
		titleCentres = (TextView) findViewById(R.id.label_title_centre);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				final Services centre = (Services) adapter.getItem(arg2);
				showDetailview(centre);
				viewFlipper.setDisplayedChild(1);
			}
		});
		viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
		loaderView = new LoaderView(this);
		viewFlipper.addView(loaderView, 0);
		viewFlipper.setDisplayedChild(0);
		new UpdateLoadCentresTask().execute();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (viewFlipper.getDisplayedChild() > 0) {
				titleCentres.setText(" Services ");
				viewFlipper.setDisplayedChild(0);
				return true;
			}
		}

		return super.onKeyDown(keyCode, event);

	}

	private void showDetailview(final Services centres) {
		titleCentres.setText(centres.getTitle());
		imageView = (ImageView) findViewById(R.id.img_centre_detail);
		textCentre = (TextView) findViewById(R.id.label_centre_detail);
		textCentre.setText(centres.getText());
		waitingview = findViewById(R.id.progress_service_image);
		imageView.setVisibility(View.GONE);
		waitingview.setVisibility(View.VISIBLE);
		LoadImagePool.loadImage(this, centres.getImageURL(), new Handler(
				new Callback() {
					@Override
					public boolean handleMessage(Message msg) {
						imageView.setVisibility(View.VISIBLE);
						waitingview.setVisibility(View.GONE);
						if (msg.obj != null) {
							Bitmap image = (Bitmap) msg.obj;
							imageView.setBackgroundDrawable(new BitmapDrawable(
									MenuService.this.getResources(), image));

						} else {
							imageView
									.setBackgroundResource(R.drawable.no_image_nci);
						}
						return false;
					}
				}), true);

		TextView head1 = (TextView) findViewById(R.id.label_head_1);
		head1.setText(centres.getHeading());
		TextView head1Value = (TextView) findViewById(R.id.label_head_1_value);
		head1Value.setText(centres.getText1());
		Linkify.addLinks(head1Value, Linkify.EMAIL_ADDRESSES | Linkify.ALL);

	}

	private void showListview() {
		adapter = new ListServicesAdapter(listCentres, false);
		listView.setAdapter(adapter);

	}

}
